package com.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.Client;
import com.entity.Goods;
import com.entity.GoodsRepertory;
import com.entity.GoodsRepertoryDetail;
import com.entity.Market;
import com.entity.MarketDetail;
import com.service.ClientService;
import com.service.GoodsRepertoryDetailService;
import com.service.GoodsRepertoryService;
import com.service.GoodsService;
import com.service.MarketDetailService;
import com.service.MarketService;

@Controller
public class XiaoshouController {
	Map<String, String> map=new HashMap<String, String>();
	@Autowired
	MarketService market;
	@Autowired
	MarketDetailService marketdetail;
	@Autowired
	ClientService clientService;
	@Autowired
	GoodsService goodservice;
	@Autowired
	GoodsRepertoryService good;
	@Autowired
	GoodsRepertoryDetailService goodsrepertory;
	
@RequestMapping("xiaoshou")
public String toshou(Model model) {
	List<Market> list=market.selectAll();
	model.addAttribute("lists", list);
	return "developer/xiaoshoulist";
}

@RequestMapping("xiaoshouadd")
public String toadd(Model model) {
	List<Client> list=clientService.selectAll();
	List<Goods> lists=goodservice.selectAll();
	model.addAttribute("lists", list);
	model.addAttribute("list", lists);
	return "developer/xiaoshouadd";
}
@ResponseBody
@RequestMapping("listname")
public Client client(int sid) {
	Client cli=clientService.client(sid);
	return cli;
}

@ResponseBody
@RequestMapping("listnames")
public Goods goods(int sid) {
	Goods goods=goodservice.selectByPrimaryKey(sid);
			return goods;
}

@RequestMapping("appinfoaddsave")
public String insert(Market markets,MarketDetail marketss)
{
	boolean mar=market.insert(markets);
	boolean mars=marketdetail.insert(marketss);
	if(mar==true&&mars==true) {
		return "redirect:xiaoshou";
	}
	return "developer/xiaoshouadd";
}


@RequestMapping("appview")
public String tolist(int id,Model model) {
	List<MarketDetail> list=marketdetail.selectByPrimaryKey(id);
	List<Goods> lists=goodservice.selectAll();
	
	model.addAttribute("list", lists);
	model.addAttribute("lists", list);
	return "developer/xiaoshouview";
}


@RequestMapping("viewadd")
public String viewadd(MarketDetail market) {
	boolean mars=marketdetail.insert(market);
	if(mars==true) {
		return "redirect:appview?id="+market.getMarketId();
	}return "redirect:appview";
}

@RequestMapping("toupd")
public String update(int marketid) {
	market.updatemoney(marketid);
	return "redirect:xiaoshou";
}


@ResponseBody
@RequestMapping("delapp")
public Map<String, String> delapp(int marketid){
	boolean a=market.deleteByPrimaryKey(marketid);
	boolean b=marketdetail.deleteByPrimaryKey(marketid);
	if(a==true&&b==true) {
		map.put("delResult", "true");
	}
	return map;
}

@ResponseBody
@RequestMapping("modify")
public Map<String, String> modify(int marketid,String goodsname) {
	Date day=new Date();
	List<MarketDetail> lists=marketdetail.selectByPrimaryKey(marketid);
	GoodsRepertoryDetail goods=new GoodsRepertoryDetail();
	for (MarketDetail marketDetails : lists) {
		String marketDetailss=marketDetails.getGoodsname();
		GoodsRepertory goodss=good.selectByPrimaryKeys(marketDetailss);
		goods.setGoodsId(goodss.getGoodsId()); 
		goods.setGoodsname(marketDetails.getGoodsname());
		goods.setRucount(0-marketDetails.getCount());
		goods.setShengdata(day);
		goods.setShidata(day);
		goods.setIsreturn(1);
	   int add=goodsrepertory.insert(goods);
	   good.updateCount(goodss.getGoodsId());
	}
	
	market.updatetai(marketid);
	marketdetail.updatetai(marketid);
	map.put("delResult", "true");
	return map;
}

@ResponseBody
@RequestMapping("deldetail")
public Map<String, String> deldetail(int  marketid){
	boolean del=marketdetail.deldetail(marketid);
	if(del==true) {
		map.put("delResult", "true");
	}
	return map;
}

@ResponseBody
@RequestMapping("return")
public Map<String, String> returns(String goodsname,int marketodd){
	marketdetail.updatereturn(marketodd);
	good.updatereturn(goodsname);
	map.put("delResult", "true");
	return map;
}

@ResponseBody
@RequestMapping("returninfo")
public List<MarketDetail> returninfo(String goodsname) {
	List<MarketDetail> list=marketdetail.list(goodsname);
	for(int i=0;i<list.size();i++) {
		int id=list.get(i).getMarketId();
		Market markets=market.clientname(list.get(i).getMarketId());
		String names=markets.getClientName();
		list.get(i).setClientname(markets.getClientName());
		String name=list.get(i).getClientname();
	}
	return list;
	
}

@ResponseBody
@RequestMapping("returnque")
public Map<String, String> returnque(String goodsname,int counts,int marketodd){
	GoodsRepertory goodss=good.selectByPrimaryKeys(goodsname);
	Date day=new Date();
	GoodsRepertoryDetail goods=new GoodsRepertoryDetail();
	goods.setGoodsId(goodss.getGoodsId());
	goods.setGoodsname(goodsname);
	goods.setRucount(counts);
	goods.setShengdata(day);
	goods.setShidata(day);
	goods.setIsreturn(3);
	
	int add=goodsrepertory.insert(goods);
	if(add!=0) {
		good.updateCount(goodss.getGoodsId());
		marketdetail.updatecount(marketodd);
		map.put("delResult", "true");
		
	}return map;
}
@RequestMapping("returnan")
public String returnan(String goodsname,Model model) {
	boolean upd=good.updataereturnan(goodsname);
	if(upd==true) {
		List<GoodsRepertory> list =good.selectAll();
	model.addAttribute("lists", list);
	   return "developer/kucunlist";
	}
	return "developer/kucunlist";
}

}


